package com.dc.chat.app.service.mp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.chat.entity.dao.Friend;
import com.dc.chat.entity.dao.FriendApply;
import com.dc.chat.entity.dao.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class FriendService extends ServiceImpl<FriendService.FriendMapper,Friend> {
    @Autowired
    UserService userService;
    public Collection<String> getFriendsUsername(String username) {
        User user = userService.selectByUserName(username);
        Integer id = user.getId();
        LambdaQueryWrapper<Friend> wrapper = Wrappers.lambdaQuery(new Friend());
        wrapper.eq(Friend::getUserId,id);
        List<Friend> friends = baseMapper.selectList(wrapper);
        if(friends.isEmpty()){
            return new ArrayList<>();
        }
        Set<Integer> collect = friends.stream().map(Friend::getFriendId).collect(Collectors.toSet());
        List<User> users = userService.getBaseMapper().selectBatchIds(collect);
        Set<String> usernames = users.stream().map(User::getUserName).collect(Collectors.toSet());
        return usernames;
    }

    public Collection<Integer> getFriendsId(Integer id) {
        LambdaQueryWrapper<Friend> wrapper = Wrappers.lambdaQuery(new Friend());
        wrapper.eq(Friend::getUserId,id);
        List<Friend> friends = baseMapper.selectList(wrapper);
        Set<Integer> collect = friends.stream().map(Friend::getFriendId).collect(Collectors.toSet());
        return collect;
    }

    public boolean check(Integer id, Integer id1) {
        LambdaQueryWrapper<Friend> friendLambdaQueryWrapper = Wrappers.lambdaQuery(new Friend());
        friendLambdaQueryWrapper.eq(Friend::getUserId,id);
        friendLambdaQueryWrapper.eq(Friend::getFriendId,id1);
        return baseMapper.selectOne(friendLambdaQueryWrapper)!=null;
    }

    public interface FriendMapper extends BaseMapper<Friend>{

    }
}
